﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

/// <summary>
/// Summary description for class_paciente
/// </summary>
public class class_paciente
{
    public class_paciente()
    {

    }

    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    public static void llenar_GridPacientes(string fil, string cmp, string acc, GridView GridPacientes)
    {
        Conexion conn = new Conexion();
        SqlCommand cmd = new SqlCommand("str_paciente", conn.Conn);

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@fil", fil.Trim());
        cmd.Parameters.AddWithValue("@cmp", cmp.Trim());
        cmd.Parameters.AddWithValue("@acc", acc.Trim());
        if (cmp.Trim() == "Fecha de Nacimiento")
        {
            fil = fil.Substring(0, fil.Length - 1);
            cmd.Parameters.AddWithValue("@date", Convert.ToDateTime(fil.Trim()));
        }
        else
            cmd.Parameters.AddWithValue("@date", "01/01/2009");

        cmd.Connection.Open();
        //SqlDataAdapter daap = new SqlDataAdapter(cmd);
        //DataSet ds = new DataSet("pac_filtrado");
        //daap.Fill(ds);
        SqlDataReader dr = cmd.ExecuteReader();
        GridPacientes.DataSource = dr;
        GridPacientes.DataBind();
        cmd.Connection.Close();



    }

    public static void llenar_GridPacientes(GridView GridPacientes)
    {
        Conexion conn = new Conexion();
        SqlCommand cmd = new SqlCommand("Select * from View_Pacientes", conn.Conn);
        cmd.Connection.Open();
        GridPacientes.DataSource = cmd.ExecuteReader(); ;
        GridPacientes.DataBind();
        cmd.Connection.Close();
    }

    public static void llenar_GridPacientes(GridView GridPacientes, string user)
    {
        Conexion conn = new Conexion();
        SqlCommand cmd = new SqlCommand("str_pac_by_user", conn.Conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@fil", user.Trim());
        cmd.Connection.Open();

        GridPacientes.DataSource = cmd.ExecuteReader();
        //GridPacientes.
        GridPacientes.DataBind();
        cmd.Connection.Close();



    }

    public static DataTable consulta_porID(int ID)
    {
        Conexion conn = new Conexion();
        SqlCommand cmd = new SqlCommand("str_conPaciente", conn.Conn);

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", ID);
        try 
	{
        SqlDataAdapter daap = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        daap.Fill(dt);
        return dt;
            	}
	catch (SqlException)
	{
		
		throw;
	}
        






    }

    public static void modificarPaciente(int ID, string nombre, string apellido, string fnac, string sexo)
    {

        Conexion conn = new Conexion();
        SqlCommand cmd = new SqlCommand("str_modPac", conn.Conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", ID);
        cmd.Parameters.AddWithValue("@nombre", nombre.Trim());
        cmd.Parameters.AddWithValue("@apellido", apellido.Trim());
        cmd.Parameters.AddWithValue("@fnac", Convert.ToDateTime(fnac.Trim()));
        cmd.Parameters.AddWithValue("@sexo", sexo);

        try
        {
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
        }
        catch (SqlException)
        {
            throw;
        }
        finally
        {
            if (cmd.Connection.State == ConnectionState.Open)
            {
                cmd.Connection.Close();
            }
        }
        


    }

    public static void insertarPaciente(string nombre, string apellido, string fnac, string sexo, string user)
    {
        Conexion conn = new Conexion();
        SqlCommand cmd = new SqlCommand("str_insPac", conn.Conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@nombre", nombre.Trim());
        cmd.Parameters.AddWithValue("@apellido", apellido.Trim());
        cmd.Parameters.AddWithValue("@fnac", Convert.ToDateTime(fnac.Trim()));
        cmd.Parameters.AddWithValue("@sexo", sexo);
        cmd.Parameters.AddWithValue("@username", user);
        
        try
        {
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
        }
        catch (SqlException)
        {
            throw;
        }
        finally
        {
            if (cmd.Connection.State == ConnectionState.Open)
            {
                cmd.Connection.Close();
            }
        }
    }
}
